package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.MonthCommentAdapter;
import com.approval.approvalapp.center.bean.MonthCommentBean;
import com.approval.approvalapp.center.bean.ProvinceCensusBean;
import com.approval.approvalapp.util.AppConfig;
import com.approval.approvalapp.util.DataUtilGetTime;
import com.approval.approvalapp.util.DateUtils;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MonthCommentActivity extends BaseActivity {

    private MonthCommentAdapter adapter;

    public static void goInto(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MonthCommentActivity.class);
        context.startActivity(intent);
    }


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.tv_last_month)
    TextView tvLastMonth;
    @Bind(R.id.tv_select_month)
    TextView tvSelectMonth;
    @Bind(R.id.tv_next_month)
    TextView tvNextMonth;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout em;

    List<MonthCommentBean> mList = new ArrayList<>();
    int currentMonth = 0;
    int currentYear = 0;

    @Override
    public int getLayoutId() {
        return R.layout.month_comment_layout;
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MonthCommentAdapter(mList, this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Calendar calendar = Calendar.getInstance();
        int mCurrYear = calendar.get(Calendar.YEAR);
        int mCurrMonth = calendar.get(Calendar.MONTH);
        currentMonth = mCurrMonth;
        currentYear = mCurrYear;
        tvSelectMonth.setText(mCurrYear + "年" + (mCurrMonth + 1) + "月");
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData(null);
                    }
                }, 500);
            }
        });
        getData(null);
    }

    public void getData(Date date) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.ORG_CODE, "-1"));
        jsonObject.put("region_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
        if (date == null) {
            jsonObject.put("start", DataUtilGetTime.getMonthStart());
            jsonObject.put("end", DataUtilGetTime.getMonthEnd());
        } else {
            jsonObject.put("start", DataUtilGetTime.getMonthStart(date));
            jsonObject.put("end", DataUtilGetTime.getMonthEnd(date));
        }
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/evaluation_list", jsonObject.toString(), new NetDownResponse() {

            @Override
            public void success(String str) {

                Gson gson = new Gson();
                Type type = new TypeToken<List<MonthCommentBean>>() {
                }.getType();
                List<MonthCommentBean> list = gson.fromJson(str, type);
                if ( list.size() > 0) {
                    em.setVisibility(View.GONE);
                    mList.clear();
                    mList.addAll(list);
                    adapter.notifyDataSetChanged();
                } else {
                    em.setVisibility(View.VISIBLE);
                    mList.clear();
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                if (swipeRefreshLayout != null) {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });

    }

    private TimePickerView pvCustomLunar;

    private void initLunarPicker(Calendar mCurrentDate) {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1949, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2069, 2, 28);
        //时间选择器 ，自定义布局
        pvCustomLunar = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

                getData(date);
                String time = getTime(date);
                tvSelectMonth.setText(time);
            }
        })
                .setDate(mCurrentDate)

                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.returnData();
                                pvCustomLunar.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.dismiss();
                            }
                        });
                    }

                })
                .setType(new boolean[]{true, true, false, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.RED)
                .build();
        pvCustomLunar.show();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
        return format.format(date);
    }


    @OnClick({R.id.tv_last_month, R.id.tv_select_month, R.id.tv_next_month, R.id.back_iv})
    public void onViewClicked(View view) {
        Calendar mCurrentDate = Calendar.getInstance();
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.tv_next_month:
                currentMonth++;
                if (currentMonth > 11) {
                    currentYear++;
                    currentMonth = 0;
                }
                mCurrentDate.set(currentYear, currentMonth, 10);
                getData(mCurrentDate.getTime());
                tvSelectMonth.setText(currentYear + "年" + (currentMonth + 1) + "月");
                break;
            case R.id.tv_select_month:
                mCurrentDate.set(currentYear, currentMonth, 10);
                initLunarPicker(mCurrentDate);
                break;
            case R.id.tv_last_month:
                currentMonth--;
                if (currentMonth < 0) {
                    currentYear--;
                    currentMonth = 11;
                }
                mCurrentDate.set(currentYear, currentMonth, 10);
                getData(mCurrentDate.getTime());
                tvSelectMonth.setText(currentYear + "年" + (currentMonth + 1) + "月");
                break;
        }
    }


}
